class CitiesController < ApplicationController

  #-------------------------------------------------------------------------
  # GET /cities
  # GET /cities.json
  #-------------------------------------------------------------------------
  def index
    @cities = City.all

	#@json = @cities.to_gmaps4rails 				#cities
	#@json = City.all.to_gmaps4rails
								
	@json = @cities.to_gmaps4rails do |city, marker|
		marker.infowindow render_to_string(
			:partial => "/cities/infowindow", 
			:locals => { :city => city}
			)		
		marker.title "#{city.name}"
		marker.json({ :population => city.population})
		marker.picture(
		{
			:picture	=> "/assets/marker.png",  #OSS: 'marker.png' si trova nella cartella ASSETS/IMAGES
			:width 		=> 32,
			:height 	=> 32,
			
			#:tooltip => "this is a sample tooltip" #### this is for tooltip   
		})
	end
	
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @cities }
    end
  end

  #-------------------------------------------------------------------------
  # GET /cities/1
  # GET /cities/1.json
  #-------------------------------------------------------------------------
  def show
	@city = City.find(params[:id])

									 @json = @city.to_gmaps4rails 				#city
				
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @city }
    end
  end

  #-------------------------------------------------------------------------
  # GET /cities/new
  # GET /cities/new.json
  #-------------------------------------------------------------------------
  
  def new
    @city = City.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @city }
    end
  end

  #-------------------------------------------------------------------------
  # GET /cities/1/edit
  #-------------------------------------------------------------------------
  
  def edit
    @city = City.find(params[:id])
  end

  #-------------------------------------------------------------------------
  # POST /cities
  # POST /cities.json
  #-------------------------------------------------------------------------
  
  def create
    @city = City.new(params[:city])

    respond_to do |format|
      if @city.save
        format.html { redirect_to @city, notice: 'City was successfully created.' }
        format.json { render json: @city, status: :created, location: @city }
      else
        format.html { render action: "new" }
        format.json { render json: @city.errors, status: :unprocessable_entity }
      end
    end
  end

  #-------------------------------------------------------------------------
  # PUT /cities/1
  # PUT /cities/1.json
  #-------------------------------------------------------------------------
  def update
    @city = City.find(params[:id])

    respond_to do |format|
      if @city.update_attributes(params[:city])
        format.html { redirect_to @city, notice: 'City was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @city.errors, status: :unprocessable_entity }
      end
    end
  end

  #-------------------------------------------------------------------------
  # DELETE /cities/1
  # DELETE /cities/1.json
  #-------------------------------------------------------------------------
  def destroy
    @city = City.find(params[:id])
    @city.destroy

    respond_to do |format|
      format.html { redirect_to cities_url }
      format.json { head :no_content }
    end
  end
end
